
package edu.towson.cosc.cosc455.biedermann.compiler;

import java.util.Stack;

public class MySemanticAnalyzer {

	/**
	 * Stack created to maintain scoping blocks
	 * @see #pretranslator
	 */
	public Stack <String> pretranslator = new Stack<String>();
	
	
	/**
	 * Method to translate each token in the parsed file to html
	 * checks if regular text, pushes on stack to maintain scope, adds to final list
	 * checks if begin tag, translates, pushes on stack to maintain scope, adds to final list
	 * checks if end tag, calls {@link #popMethod()} to maintain scope and apply meaning, adds to final list
	 * checks if define variable, adds variable name and meaning to variableList
	 * checks if using a variable, adds variable meaning to final list
	 * @see #translate()
	 */
	public void translate(){
		for(int j = 0 ; j<MyCompiler.parsedInput.size(); j++){
			
			if(LexicalTokens.textList.contains(MyCompiler.parsedInput.get(j))){
				LexicalTokens.finalText.add(MyCompiler.parsedInput.get(j));
				pretranslator.push(MyCompiler.parsedInput.get(j));
			}
			
			if(LexicalTokens.beginTags.contains(MyCompiler.parsedInput.get(j))){
				LexicalTokens.finalText.add(htmlBeginTerm(MyCompiler.parsedInput.get(j)));
				pretranslator.push(htmlBeginTerm(MyCompiler.parsedInput.get(j)));
			}

			if(LexicalTokens.endTags.contains(MyCompiler.parsedInput.get(j))){
				String s = popMethod();
				LexicalTokens.finalText.add(htmlEndTerm(s));	
			}
			
			if(LexicalTokens.DEFINE_VAR_BEGIN.equals(MyCompiler.parsedInput.get(j))){
				pretranslator.push(MyCompiler.parsedInput.get(j));
				j++;
				LexicalTokens.variableList.add(MyCompiler.parsedInput.get(j));
				j=j+2;
				LexicalTokens.variableList.add(MyCompiler.parsedInput.get(j));
				j++;
			}
			
			if(LexicalTokens.VARIABLE_BEGIN.equals(MyCompiler.parsedInput.get(j))){
				j++;
				String getVariable = MyCompiler.parsedInput.get(j);
				if(!LexicalTokens.variableList.contains(getVariable)){
					System.err.println("Semantic Error: "+ "\""+getVariable+"\"" +" is not a defined variable");
					System.exit(0);
				}
				for(int n= (LexicalTokens.variableList.size()-1); n>=0; n--){
					if(getVariable.equals(LexicalTokens.variableList.get(n))){
						LexicalTokens.finalText.add(LexicalTokens.variableList.get(n+1));
						break;			
					}
				}
				j++;
			}
		}
		
		//Test to ensure every token was properly translated
		/*System.out.println("now printing final");
		for(int t = 0; t<LexicalTokens.finalText.size(); t++){
			System.out.println(LexicalTokens.finalText.get(t));
		}*/
	}
	
	/**
	 * Method pops until a scope block is completed
	 * pops until begin tag is found, returns begin tag to apply corresponding meaning to end tag
	 * if define tag is found, 
	 * the occurrence of the variable name and meaning is deleted from the variableList to maintain scope of variable
	 * @return tempString
	 */
	public String popMethod(){
		String tempString= "";
		
		while(!LexicalTokens.beginHtmlTags.contains(tempString) || tempString == "<html>"){
			if(tempString=="<html>"){
				break;
			}
			if(tempString.equals(LexicalTokens.DEFINE_VAR_BEGIN)){
				LexicalTokens.variableList.remove(LexicalTokens.variableList.size()-1); //REMOVING KIM
				LexicalTokens.variableList.remove(LexicalTokens.variableList.size()-1);// REMOVING NAME
			}
			tempString = pretranslator.pop();
		}
		
		return tempString;
	}
	
	/**
	 * Method to apply html meaning to lolspeak begin tag
	 * uses a map to define html begin by using the corresponding lolspeak begin tag
	 * @param x
	 * @return #htmB
	 */
	
	public String htmlBeginTerm(String x){
		String htmlB = LexicalTokens.lolToHtml.get(x);
		
		return htmlB;	
	}
	
	/**
	 * Method to apply html meaning to lolspeak end tag
	 * Uses a map to define the html ending by using the the corresponding html beginning
	 * @param x
	 * @return htmlE
	 */
	public String htmlEndTerm(String x){
		String htmlE = LexicalTokens.htmlBegintoEnd.get(x);
		return htmlE;
	}
}